package com.qimai;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

public class QiMaiEnc {
//    private final static String b = "00000008d78d46a";
    private final static String b = "0000000c735d856";

    private static long getE(){
        return System.currentTimeMillis() - 357 - 1515125653845L;
    }


    private static String encrypt_param(String a) {
        String[] aArr = a.split("");
        int t = a.length(), e = b.length(),  i = 0;
        for (;i < t; i++) {
            aArr[i] = "" + (char) (aArr[i].charAt(0) ^ b.charAt((i + 10) % e));
        }
        return StringUtils.join(aArr);
    }


    private static String base_encode(String a) {
        return BytesHexStrTranslate.base64(a);
    }

    public static String getAnalysis(String[] value_list, String url) {
//        Arrays.sort(value_list);
//        String p1 = StringUtils.join(value_list, "");
        String p1 = Arrays.stream(value_list).filter(Objects::nonNull).sorted().collect(Collectors.joining());
        String m = base_encode(p1);
        m += "@#" + url;
        m += "@#" + getE();
        m += "@#" + "1";

        return base_encode(encrypt_param(m));
    }

    public static void main(String[] args) {
        // eB5iX1RdVElSA35KAmAGWwVgd15qcQ0NcBMfEVZdXktRW1JVSHATAQIDUQUHA1wKAQEFcBMB
        // dR5uXANdVElSA35KWTMCVgVsdAlqcQ0NcBMfSgVZU0tdWAVVSHATAQIDClYDDlwGAlYFcBMB
        System.out.println(base_encode(encrypt_param("MzZjbmdyb3NzaW5naXBhZA==@#/rank/index@#123224682475@#1")));
        System.out.println(base_encode("36cnipadpaid"));


    }
}